package com.jpcc.vw.onlineRadio.model.impl;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.jpcc.vw.onlineRadio.api.RadioService;
import com.jpcc.vw.onlineRadio.application.MusicApplication;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.AlbumBrowseBean;
import com.jpcc.vw.onlineRadio.model.bean.AlbumBrowseBeanNew;
import com.jpcc.vw.onlineRadio.model.bean.AlbumBrowseDataBean;
import com.jpcc.vw.onlineRadio.model.bean.AlbumCategoryBean;
import com.jpcc.vw.onlineRadio.model.bean.AlbumCategoryResultBean;
import com.jpcc.vw.onlineRadio.model.bean.AlbumFavouriteBean;
import com.jpcc.vw.onlineRadio.model.bean.AllSearchResult;
import com.jpcc.vw.onlineRadio.model.bean.Announcer;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.BestCollectionBean;
import com.jpcc.vw.onlineRadio.model.bean.BoughtAlbumBean;
import com.jpcc.vw.onlineRadio.model.bean.CPInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.CategoryTagBean;
import com.jpcc.vw.onlineRadio.model.bean.CommonAlbumRadioSearchResultBean;
import com.jpcc.vw.onlineRadio.model.bean.CommonFavouritePaiedHistoryEntity;
import com.jpcc.vw.onlineRadio.model.bean.DailyRecommendationBean;
import com.jpcc.vw.onlineRadio.model.bean.ExclusiveRecommendationBean;
import com.jpcc.vw.onlineRadio.model.bean.HotwordsBean;
import com.jpcc.vw.onlineRadio.model.bean.LiveRadioDetailBean;
import com.jpcc.vw.onlineRadio.model.bean.LocalOnlineRadioBean;
import com.jpcc.vw.onlineRadio.model.bean.NewAlbumAndRadiosData;
import com.jpcc.vw.onlineRadio.model.bean.NewRecommendBean;
import com.jpcc.vw.onlineRadio.model.bean.NewSearchAlbumResultBean;
import com.jpcc.vw.onlineRadio.model.bean.OnlineRadioCityBean;
import com.jpcc.vw.onlineRadio.model.bean.OnlineRadioSimpleInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.PaiedAlbumBean;
import com.jpcc.vw.onlineRadio.model.bean.PlaceOrderBean;
import com.jpcc.vw.onlineRadio.model.bean.PlayHistoryOnlineBean;
import com.jpcc.vw.onlineRadio.model.bean.PlayInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.PriceTypeInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.RadioDeailBean;
import com.jpcc.vw.onlineRadio.model.bean.RecommendationEntity;
import com.jpcc.vw.onlineRadio.model.bean.SearchAllBean;
import com.jpcc.vw.onlineRadio.model.bean.SearchHistoryResponseBean;
import com.jpcc.vw.onlineRadio.model.bean.SimpleAlbumRadioEntity;
import com.jpcc.vw.onlineRadio.model.bean.SubordinatedAlbum;
import com.jpcc.vw.onlineRadio.model.bean.SuggestWordsBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListAlbumBean;
import com.jpcc.vw.onlineRadio.model.bean.TopListBean;
import com.jpcc.vw.onlineRadio.model.bean.Track;
import com.jpcc.vw.onlineRadio.model.bean.VIPInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.VIPRecommendationBean;
import com.jpcc.vw.onlineRadio.model.bean.XiMaLaYaOrderBean;
import com.jpcc.vw.onlineRadio.utils.ServiceFactory;
import com.jpcc.vw.onlineRadio.utils.TransformUtils;
import com.jpcc.vw.onlineRadio.model.bean.NewSearchAlbumResultBean.DataBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Query;
import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;

public class RadioImpl implements IRadio {

    /**
     * 获取经纬度下的城市code
     *
     * @return
     */
    @Override
    public Observable<OnlineRadioCityBean> GetRadioCode(String latitude, String longitude) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetRadioCode(latitude, longitude).compose(TransformUtils.<OnlineRadioCityBean>defaultSchedulers());
    }

    @Override
    public Observable<OnlineRadioSimpleInfoBean> GetRadiosByCity(int cityCode, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetRadiosByCity(cityCode, Constants.PAGE_NUM, pageIndex).compose(TransformUtils.<OnlineRadioSimpleInfoBean>defaultSchedulers());
    }

    @Override
    public Observable<ExclusiveRecommendationBean> GetExclusiveRecommendations() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetExclusiveRecommendations(50).compose(TransformUtils.<ExclusiveRecommendationBean>defaultSchedulers());
    }

    /**
     * 获取每日推荐
     *
     * @return
     */
    @Override
    public Observable<DailyRecommendationBean> GetDailyRecommendations() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetDailyRecommendations(5).compose(TransformUtils.<DailyRecommendationBean>defaultSchedulers());
    }

    @Override
    public Observable<AlbumCategoryBean> GetAlbumCategory() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetAlbumCategory().compose(TransformUtils.<AlbumCategoryBean>defaultSchedulers());
    }

    /**
     * 根据标签获取专辑列表
     *
     * @param categoryId
     * @param page
     * @return
     */
    @Override
    public Observable<AlbumCategoryResultBean> GetAlbumByCategory(long categoryId, int page, String tagName) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetAlbumByCategory(categoryId, 1, 20, page, tagName).compose(TransformUtils.<AlbumCategoryResultBean>defaultSchedulers());
    }

    /**
     * 获取用户收藏列表并于本地同步
     *
     * @return
     */
    @Override
    public Observable<AlbumFavouriteBean> GetAlbumRadioFavourite() {
        return Observable.zip(ServiceFactory.GetInstance().createService(RadioService.class).GetAlbumRadioFavourite(MusicApplication.GetUserId(), 0), ServiceFactory.GetInstance().createService(RadioService.class).GetAlbumRadioFavourite(MusicApplication.GetUserId(), 2), new Func2<AlbumFavouriteBean, AlbumFavouriteBean, AlbumFavouriteBean>() {
            @Override
            public AlbumFavouriteBean call(AlbumFavouriteBean albumFavouriteBeanAlbum, AlbumFavouriteBean albumFavouriteBean2Radio) {
                if (albumFavouriteBeanAlbum.getCode() == 0 && albumFavouriteBean2Radio.getCode() == 0) {
                    AlbumFavouriteBean resultFavouriteBean = new AlbumFavouriteBean();
                    List<CommonFavouritePaiedHistoryEntity> data = new ArrayList<>();
                    resultFavouriteBean.setDataALL(data);
                    if (albumFavouriteBeanAlbum.getData() != null && albumFavouriteBeanAlbum.getData().getAlbums()!=null&&albumFavouriteBeanAlbum.getData().getAlbums().size()>0) {
                        for (int index = albumFavouriteBeanAlbum.getData().getAlbums().size() - 1; index >= 0; index--) {
                            resultFavouriteBean.getDataALL().add(albumFavouriteBeanAlbum.getData().getAlbums().get(index));
                        }
                    }
                    if (albumFavouriteBean2Radio.getData() != null && albumFavouriteBean2Radio.getData().getRadios()!=null&&albumFavouriteBean2Radio.getData().getRadios().size() > 0) {

                        for (int index = albumFavouriteBean2Radio.getData().getRadios().size() - 1; index >= 0; index--) {
                            resultFavouriteBean.getDataALL().add(albumFavouriteBean2Radio.getData().getRadios().get(index));
                        }
                    }
                    return resultFavouriteBean;
                } else {
                    AlbumFavouriteBean resultFavouriteBean = new AlbumFavouriteBean();
                    resultFavouriteBean.setCode(-1);
                    return resultFavouriteBean;
                }
            }
        }).compose(TransformUtils.<AlbumFavouriteBean>defaultSchedulers());
    }

    /**
     * 获取所有已经购买的列表
     *
     * @param pageIndex
     * @return
     */
    @Override
    public Observable<PaiedAlbumBean> GetAllPaiedAlbum(int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetAllPaiedAlbum(MusicApplication.GetUserId(), Constants.PAGE_NUM, pageIndex).compose(TransformUtils.<PaiedAlbumBean>defaultSchedulers());
    }

    @Override
    public Observable<AlbumBrowseBean> GetAlbumBrowse(long albumId, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetAlbumBrowse(albumId, "asc", Constants.PAGE_NUM, pageIndex).flatMap(new Func1<AlbumBrowseBeanNew, Observable<AlbumBrowseBean>>() {
            @Override
            public Observable<AlbumBrowseBean> call(AlbumBrowseBeanNew albumBrowseBeanNew) {
                AlbumBrowseBean responseAlbumBrowseBean = new AlbumBrowseBean();
                if (albumBrowseBeanNew != null) {
                    responseAlbumBrowseBean.setCode(albumBrowseBeanNew.getCode());
                    responseAlbumBrowseBean.setStatusMessage(albumBrowseBeanNew.getStatusMessage());
                    AlbumBrowseDataBean albumData = new AlbumBrowseDataBean();
                    List<Track> tracks = new ArrayList<>();
                    if (albumBrowseBeanNew.getData() != null && albumBrowseBeanNew.getData().getPageData() != null) {
                        if (albumBrowseBeanNew.getData().getPageData().size() > 0) {
                            albumData.setAlbumId(albumBrowseBeanNew.getData().getPageData().get(0).getId());
                            albumData.setAlbumTitle(albumBrowseBeanNew.getData().getPageData().get(0).getTitle());
                        }
                        for (AlbumBrowseBeanNew.DataBean.PageDataBean data : albumBrowseBeanNew.getData().getPageData()) {
                            Track track = new Track();
                            track.setId(data.getId());
                            track.setTrackTitle(data.getTitle());
                            track.setKind(data.getKind());
                            track.setCoverUrlSmall(data.getImage().getUrl());
                            track.setCoverUrlMiddle(data.getImage().getUrl());
                            track.setCoverUrlLarge(data.getImage().getUrl());
                            Announcer announcer = new Announcer();
                            announcer.setId(data.getAnnouncer().getId());
                            announcer.setNickname(data.getAnnouncer().getNickname());
                            track.setAnnouncer(announcer);
                            track.setDuration(data.getDuration());
                            track.setPlayUrl32(data.getPlay_info().getPlay_32().getUrl());
//                            track.setPlayUrl32("http://audio.pay.xmcdn.com/download/1.0.0/group1/M00/0B/69/wKgJMlmSsMqxWeqkAA0HvOFLEdc683.m4a?sign=6e143e431522053f5849630d5128f0d0&buy_key=333332653336333333323337333133303338333633343334333033333334333033373264333032653334333333343339333833353338333133393339333233373337333133313331&timestamp=1551244565149000&token=6149&duration=275");
                            track.setPlaySize32(data.getPlay_info().getPlay_32().getSize());
                            track.setPlayUrl64(data.getPlay_info().getPlay_64().getUrl());
                            track.setPlaySize64(data.getPlay_info().getPlay_64().getSize());
                            track.setCanDownload(data.isCan_download());
                            track.setDownloadUrl(data.getPlay_info().getDownload_url().getUrl());
                            track.setDownloadSize(data.getPlay_info().getDownload_url().getSize());
                            SubordinatedAlbum subordinatedAlbum = new SubordinatedAlbum();
                            subordinatedAlbum.setId(albumId);
                            subordinatedAlbum.setAlbumTitle(data.getAlbum().getTitle());
                            subordinatedAlbum.setCoverUrlLarge(data.getImage().getUrl());
                            subordinatedAlbum.setCoverUrlMiddle(data.getImage().getUrl());
                            subordinatedAlbum.setCoverUrlSmall(data.getImage().getUrl());
                            track.setSubordinatedAlbum(subordinatedAlbum);
                            track.setType("album");
                            track.setLive(false);
                            track.setIs_authorized(data.isIs_authorized());
                            track.setIs_free(data.isIs_free());
                            track.setIs_paid(data.isIs_paid());
                            tracks.add(track);
                        }
                        albumData.setTracks(tracks);
                    }
                    responseAlbumBrowseBean.setData(albumData);
                } else {
                    responseAlbumBrowseBean.setCode(-1);
                }
                return Observable.just(responseAlbumBrowseBean);
            }
        }).compose(TransformUtils.<AlbumBrowseBean>defaultSchedulers());
    }

    @Override
    public Observable<NewSearchAlbumResultBean> SearchAlbums(String q, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).SearchAlbums(q, Constants.PAGE_NUM, pageIndex, true).compose(TransformUtils.<NewSearchAlbumResultBean>defaultSchedulers());
    }

    /**
     * 获取电台直播节目的详细信息
     *
     * @param radioId
     * @return
     */
    @Override
    public Observable<LiveRadioDetailBean> GetPlayingProgramDetail(long radioId) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetPlayingProgramDetail(radioId).compose(TransformUtils.<LiveRadioDetailBean>defaultSchedulers());
    }

    @Override
    public Observable<RadioDeailBean> getLiveSchedules(long radioId, int weekday) {
        return ServiceFactory.GetInstance().createService(RadioService.class).getLiveSchedules(radioId, weekday).compose(TransformUtils.<RadioDeailBean>defaultSchedulers());
    }

    @Override
    public Observable<PlayHistoryOnlineBean> getPlayHistory() {
        return ServiceFactory.GetInstance().createService(RadioService.class).getPlayHistory().compose(TransformUtils.<PlayHistoryOnlineBean>defaultSchedulers());
    }

    @Override
    public Observable<HotwordsBean> GetHotWords() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetHotWords(6).compose(TransformUtils.<HotwordsBean>defaultSchedulers());
    }

    @Override
    public Observable<SuggestWordsBean> GetSearchSuggestWords(String q) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetSearchSuggestWords(q).compose(TransformUtils.<SuggestWordsBean>defaultSchedulers());
    }

    @Override
    public Observable<LocalOnlineRadioBean> BrowseLocalRadios(String latitude, String longitude,int pageIndex) {
        if (Double.parseDouble(latitude) == 0 && Double.parseDouble(longitude) == 0) {
            return ServiceFactory.GetInstance().createService(RadioService.class).BrowseLocalRadios(pageIndex,10).compose(TransformUtils.<LocalOnlineRadioBean>defaultSchedulers());
        } else {
            return ServiceFactory.GetInstance().createService(RadioService.class).BrowseLocalRadios(latitude, longitude,pageIndex,10).compose(TransformUtils.<LocalOnlineRadioBean>defaultSchedulers());
        }
    }

    @Override
    public Observable<SearchHistoryResponseBean> GetSearchHistory() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetSearchHistory().compose(TransformUtils.<SearchHistoryResponseBean>defaultSchedulers());
    }

    @Override
    public Observable<PriceTypeInfoBean> GetPriceInfo(long albumId) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetPriceInfo(albumId).compose(TransformUtils.<PriceTypeInfoBean>defaultSchedulers());
    }

    @Override
    public Observable<CPInfoBean> GetCPInfo() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetCPInfo().compose(TransformUtils.<CPInfoBean>defaultSchedulers());
    }

    @Override
    public Observable<CategoryTagBean> GetCategoryTags(long categoryId) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetCategoryTags(categoryId, 0).compose(TransformUtils.<CategoryTagBean>defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> PlaceOrder(int priceType, String payContent, String price) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("priceType", priceType + "");
        hashMap.put("payContent", payContent + "");
        hashMap.put("price", price + "");
        StringBuffer data = new StringBuffer();
        if (hashMap != null && hashMap.size() > 0) {
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                data.append(key).append("=").append(val).append("&");
            }
        }
        String jso = data.substring(0, data.length() - 1);


        PlaceOrderBean order = new PlaceOrderBean();
        order.setPriceType(priceType);
        order.setPrice(price);
        order.setPayContent(Long.parseLong(payContent));


        RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), jso);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(new Gson().toJson(order));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ServiceFactory.GetInstance().createService(RadioService.class).PlaceOrder(jsonObject).compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    @Override
    public Observable<BoughtAlbumBean> GetBoughtAlbums() {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetBoughtAlbums().compose(TransformUtils.<BoughtAlbumBean>defaultSchedulers());
    }

    @Override
    public Observable<ExclusiveRecommendationBean> GetRecommendation(int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetRecommendation("album", "android", "862446000000312", pageIndex, Constants.PAGE_NUM).flatMap(new Func1<NewRecommendBean, Observable<ExclusiveRecommendationBean>>() {
            @Override
            public Observable<ExclusiveRecommendationBean> call(NewRecommendBean newRecommendBean) {
                ExclusiveRecommendationBean exclusiveRecommendationBean = new ExclusiveRecommendationBean();
                exclusiveRecommendationBean.setCode(newRecommendBean.getCode());
                exclusiveRecommendationBean.setStatusMessage(newRecommendBean.getStatusMessage());
                List<RecommendationEntity> data = new ArrayList<>();
                if (newRecommendBean.getData() != null && newRecommendBean.getData().getPageData() != null) {
                    for (NewRecommendBean.DataBean.PageDataBean pageData : newRecommendBean.getData().getPageData()) {
                        RecommendationEntity entity = new RecommendationEntity();
                        entity.setId(pageData.getId());
                        entity.setName(pageData.getTitle());
                        entity.setHostName("");
//                        entity.setCoverUrlSmall(pageData.getCover().getLarge().getUrl());
//                        entity.setCoverUrlMiddle(pageData.getCover().getLarge().getUrl());
//                        entity.setCoverUrlLarge(pageData.getCover().getLarge().getUrl());
                        entity.setCover(pageData.getCover());
                        entity.setUpdateTime(pageData.getUpdated_at());
                        data.add(entity);
                    }
                }
                if (newRecommendBean.getData() != null) {
                    exclusiveRecommendationBean.setTotalPage(newRecommendBean.getData().getTotalPage());
                }
                exclusiveRecommendationBean.setData(data);
                return Observable.just(exclusiveRecommendationBean);
            }
        }).compose(TransformUtils.<ExclusiveRecommendationBean>defaultSchedulers());
    }

    @Override
    public Observable<AlbumBrowseBean> BrowsePaidAlbumTracks(long albumId, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).BrowsePaidAlbumTracks(albumId, "asc", Constants.PAGE_NUM, pageIndex, false).compose(TransformUtils.<AlbumBrowseBean>defaultSchedulers());
    }

    @Override
    public Observable<TopListBean> RadioRanks(int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).RadioRanks("album", pageIndex, Constants.PAGE_NUM).compose(TransformUtils.<TopListBean>defaultSchedulers());
    }

    @Override
    public Observable<TopListAlbumBean> RadioRanksById(int id, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).RadioRanksById(id + "", id, pageIndex, Constants.PAGE_NUM).compose(TransformUtils.<TopListAlbumBean>defaultSchedulers());
    }

    @Override
    public Observable<VIPInfoBean> QueryVIPInfo() {
        return ServiceFactory.GetInstance().createService(RadioService.class).QueryVIPInfo().compose(TransformUtils.<VIPInfoBean>defaultSchedulers());
    }

    @Override
    public Observable<BestCollectionBean> BestCollection(int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).BestCollection(pageIndex,Constants.PAGE_NUM).compose(TransformUtils.<BestCollectionBean>defaultSchedulers());
    }

    @Override
    public Observable<VIPRecommendationBean> VIPRecommendation(int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).VIPRecommendation(pageIndex,Constants.PAGE_NUM).compose(TransformUtils.<VIPRecommendationBean>defaultSchedulers());
    }

    @Override
    public Observable<PlayInfoBean> GetPlayInfo(long trackId) {
        return ServiceFactory.GetInstance().createService(RadioService.class).GetPlayInfo(trackId).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> ClearSearchHistory() {
        return ServiceFactory.GetInstance().createService(RadioService.class).ClearSearchHistory().compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    @Override
    public Observable<CommonAlbumRadioSearchResultBean> SearchAll(String q, int pageSize, int pageIndex) {
        return ServiceFactory.GetInstance().createService(RadioService.class).SearchAll(q, Constants.PAGE_NUM, pageIndex,2).flatMap(new Func1<NewAlbumAndRadiosData, Observable<CommonAlbumRadioSearchResultBean>>() {
            @Override
            public Observable<CommonAlbumRadioSearchResultBean> call(NewAlbumAndRadiosData searchAllBean) {
                CommonAlbumRadioSearchResultBean response=new CommonAlbumRadioSearchResultBean();
                List<SimpleAlbumRadioEntity> albums=new ArrayList<>();
                List<SimpleAlbumRadioEntity> radios=new ArrayList<>();
                response.setAlbums(albums);
//                response.setRadios(radios);
                if(searchAllBean.getCode()==0){
                    if(searchAllBean.getData()!=null&&searchAllBean.getData().getAlbumsAndRadios()!=null&&searchAllBean.getData().getAlbumsAndRadios().getAlbumList()!=null&&searchAllBean.getData().getAlbumsAndRadios().getAlbumList().getPageData()!=null&&searchAllBean.getData().getAlbumsAndRadios().getAlbumList().getPageData().size()>0){
                        for(AllSearchResult.AlbumListBean.PageDataBean data:searchAllBean.getData().getAlbumsAndRadios().getAlbumList().getPageData() ){
                            SimpleAlbumRadioEntity entity=new SimpleAlbumRadioEntity();
                            entity.setType("album");
                            entity.setTitle(data.getTitle());
                            entity.setAlbumId(data.getId());
                            entity.setCover(data.getCover().getSmall().getUrl());
                            entity.setPaid(data.isIsPaid());
                            entity.setAuthorized(data.isIsAuthorized());
                            entity.setVipFree(data.isIsVipFree());
                            entity.setVipOnly(data.isIsVipOnly());
                            entity.setSample(data.isIsSample());
                            if(data.getAnnouncer()!=null&&!TextUtils.isEmpty(data.getAnnouncer().getNickname())){
                                entity.setHostName(data.getAnnouncer().getNickname());
                            }
                            else{
                                entity.setHostName("");
                            }
                            response.getAlbums().add(entity);
                        }
                    }

                    if(searchAllBean.getData()!=null&&searchAllBean.getData().getAlbumsAndRadios()!=null&&searchAllBean.getData().getAlbumsAndRadios().getRadioList()!=null&&searchAllBean.getData().getAlbumsAndRadios().getRadioList().getPageData()!=null&&searchAllBean.getData().getAlbumsAndRadios().getRadioList().getPageData().size()>0){
                        for (AllSearchResult.RadioListBean.PageDataBean data:searchAllBean.getData().getAlbumsAndRadios().getRadioList().getPageData()){
                            SimpleAlbumRadioEntity entity=new SimpleAlbumRadioEntity();
                            entity.setType("radio");
                            entity.setTitle(data.getName());
                            entity.setAlbumId(data.getId());
                            entity.setCover(data.getSmallCoverUrl());
                            response.getAlbums().add(entity);
                        }
                    }
                }
                else{
                    response.setCode(searchAllBean.getCode());
                    response.setStatusMessage(searchAllBean.getStatusMessage());
                }
                return Observable.just(response);
            }
        }).compose(TransformUtils.<CommonAlbumRadioSearchResultBean>defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> AddUserFavourite(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).AddUserFavourite(body).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> CancelUserFavourite(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).CancelUserFavourite(body).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> SyncSearchHistory(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).SyncSearchHistory(body).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> SyncPlayHistory(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).SyncPlayHistory(body).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> DeletePlayHistory(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).DeletePlayHistory(body).compose(TransformUtils.defaultSchedulers());
    }

    @Override
    public Observable<XiMaLaYaOrderBean> PlaceOrder(String jsonString) {
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
        return ServiceFactory.GetInstance().createService(RadioService.class).PlaceOrder(body).compose(TransformUtils.defaultSchedulers());
    }
}
